<?php

namespace App\Http\Controllers\Wx;

use App\Helpers\ResponseData;
use App\Models\Student;
use EasyWeChat\OfficialAccount\Application;
use Illuminate\Http\Request;
use Modules\Car\Entities\CarOrder;
use Modules\Manager\Entities\Department;
use Modules\Manager\Entities\Manager;
use EasyWeChat\Factory;

class WechatController extends WxCommonController
{
    use ResponseData;

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        $config = config('wechat.official_account.default');
        $app = Factory::officialAccount($config);
        $app->server->push(function($message){
            return "欢迎关注 邦拓租车！";
        });

        return $app->server->serve();
    }

    /**
     * @return void
     * 模板消息
     */
    public function templateList(){
        $app = $this->wechatApp;
        $list = $app->template_message->getPrivateTemplates();
        return $this->success($list);
    }
    // 测试模板消息
    public function sendTemplate(CarOrder $model,Request $request){
        $openid = $request->openid;
        $order = $model->where('status',0)->first();
        $res = $this->orderTemplate($openid,$order);
        return $this->success($res);
    }
    // 调用发送模板消息
    public function sendTemplateWxAdmin(Manager $model,Request $request){
        $manager = $model->where('department_id',$request->department_id)->where('post_id',2)->first();
        if(!$manager){
            return $this->success('店长不存在');
        }
        if(!$manager->wechat_openid){
            return $this->success('店长未绑定公众号openid');
        }
        $app = $this->wechatApp;
        $res = $app->template_message->send([
            'touser' => $manager->wechat_openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'miniprogram' => [
                'appid' => config('wechat.mini_program.default.app_id'),
                'pagepath'=> $request->page??'pages/index/index',
            ],
            'data' => [
                // 'first' => $request->first, // 已失效！！！！！！！！！！！！！！！
                'tradeDateTime'=>now(), // 提交时间 - 有效
                'orderType'=>$request->orderType, // 订单类型 - 有效
                'customerInfo'=>$request->customerInfo, // 客户信息 - 有效
                // 'orderItemName'=>$request->orderItemName, // 已失效！！！！！！！！！！！！！！！
                // 'orderItemData'=>$request->orderItemData, // 已失效！！！！！！！！！！！！！！！
                // 'remark'=>$request->remark, // 已失效！！！！！！！！！！！！！！！
            ],
        ]);
        return $res;
    }
    // 待支付订单
    public function orderTemplate($openid,$order){
        $app = $this->wechatApp;
        $res = $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '', // 提交时间
                'orderType'=>'待支付',
                'tradeDateTime' => $order->updated_at, // 订单类型
                'customerInfo' => $order->wx_name.' '.$order->phone.'订购车辆为'.$order->gear_car_name, // 客户信息
                'remark' => $this->getDepartment($order->department).'有意向客户浏览，请及时关注',
            ],
        ]);
        return $res;
    }
    // 已支付订单
    public function paidOrder($openid,$order){
        $app = $this->wechatApp;
        $res = $app->template_message->send([
            'touser' => $openid,
            'template_id' => config('wechat.official_account.default.template.waiting'),
            'data' => [
                'first' => '', // 提交时间
                'orderType'=>'已支付',
                'tradeDateTime' => $order->updated_at, // 订单类型
                'customerInfo' => $order->wx_name.' '.$order->phone.'订购车辆为'.$order->gear_car_name, // 客户信息
                'remark' => $this->getDepartment($order->department).'有新的订单,请及时联系客户',
            ],
        ]);
        return $res;
    }
    public function getDepartment($id){
       Department::query()->where('id',$id)->value('name');
    }
    //获取微信id
    public function getOpenid(Request $request){
        if($request->filled('code')){
            $oauth = $this->wechatApp->oauth;
            try{
                $wxuser = $oauth->userFromCode($request->code);
                $openid = $wxuser->getId();
                if($openid){
                    $student = Manager::query()->where('wechat_openid',$openid)->first();
                    if($student){
                        return $this->success($student->wechat_openid);
                    }
                    return $this->success($openid);
                }
            }catch (\Exception $exception){
                return $this->failed($exception->getMessage(),201);
            }
        }else{
            return $this->failed('请提交code',201);
        }
    }
    // 绑定用户openid
    public function bindManager(Manager $model, Request $request){
        $manager = $model->where('phone',$request->phone)->first();
        if(!$manager){
            return $this->failed('管理员不存在',201);
        }
        $manager->wechat_openid = $request->wechat_openid;
        $manager->save();
        return $this->success('绑定成功');
    }
    // 获取临近3小时预订单

}
